@echo off
cls

:: Installation script for the build platform
set WORKSPACE_FOLDER=E:\build\workspace

echo.
echo ################################# INIT #################################
echo "The workspace dir shoud contains Installation, ThirdParty, 
echo     NokiaFE, and QuestionsType directories from svn"
echo.
echo "Starting installation"

:: Catch Time when beginning installation script
for /f "tokens=1 delims=: " %%h in ('echo %time%') do set start_hour=%%h 2>NULL
for /f "tokens=2 delims=: " %%m in ('echo %TIME%') do set start_minu=%%m 2>NULL
for /f "tokens=3 delims=: " %%s in ('echo %TIME%') do set start_secd=%%s 2>NULL

:: Get Root folder (ie c:\, d:\, e:\)
SET mydir="%WORKSPACE_FOLDER%"
SET mydir=%mydir:\=;%
for /F "tokens=* delims=;" %%i IN (%mydir%) DO CALL :PARENT_FOLDER %%i
:PARENT_FOLDER
if "%1"=="" (GOTO :EOF)
SET ROOT=%1

:: Global data
set TOOLS_FOLDER=%ROOT%\tools
set DATA_FOLDER=%ROOT%\data
set MYSQL_FOLDER=%ROOT%\MySQL
set JAVA_FOLDER=%ROOT%\java
set WGET_FOLDER=%TOOLS_FOLDER%\wget
set UNZIP_FOLDER=%TOOLS_FOLDER%\unzip

echo "Installation will be done in root folder........... : %ROOT%\"
echo "Installation of java will be done in folder........ : %JAVA_FOLDER%\"
echo "Installation of tools will be done in folder....... : %TOOLS_FOLDER%\"
echo "Installation of mysql will be done in folder....... : %MYSQL_FOLDER%\"
echo "Installation of mysql data will be done in folder.. : %DATA_FOLDER%\"


:: Jdk data
set JDK_URL=http://gpssys.googlecode.com/files/jdk-6u21-windows-i586.exe
set JDK_LogFile=%TOOLS_FOLDER%\jdk.log
set JDK_InstallDIR=%JAVA_FOLDER%\jdk1.6.0_21
set JDK_MSIPKG=C:\Documents and Settings\%username%\Application Data\Sun\Java\jdk1.6.0_21\jdk1.6.0_21.msi
set JAVA_HOME=%JDK_InstallDIR%
set JAVA_BinDIR=%JAVA_HOME%\bin

:: Mysql data
set MySQLSERVER_InstallDIR=%MYSQL_FOLDER%\Server 5.1
set MySQLSERVER_BinDIR=%MySQLSERVER_InstallDIR%\bin
set MySQLSERVER_DataDIR=%DATA_FOLDER%\MySQL
set MySQLSERVER_LogFile=%TOOLS_FOLDER%\mysql.log
set MySQLSERVER_ServiceName=mysql
set MySQLSERVER_MyINI_FilePath_1=%WORKSPACE_FOLDER%\Installation\configuration\version_1.0\mysqld-5.1.50\my.ini
set MySQLSERVER_MyINI_FilePath_2=%MySQLSERVER_InstallDIR%\my.ini
set MySQLSERVER_URL=http://downloads.mysql.com/archives/mysql-5.1/mysql-5.1.50-win32.msi
set MySQLSERVER_MSI=mysqlserver-5.1.50-win32.msi
set MySQLSERVER_TMP_MSI=mysql-5.1.50-win32.msi
set MySQLSERVER_USER=root
set MySQLSERVER_PASSWD=agile123

:: Mysql connector data
set MySQL_Con_URL=http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.0.8.zip/from/http://artfiles.org/mysql/
set MySQL_Con_ZIP=mysql-connector-java-5.0.8.zip

:: Unzip data
set UNZIP_BINDIR=%TOOLS_FOLDER%\unzip\bin

:: Wget data
set WGET_BINDIR=%TOOLS_FOLDER%\wget\bin
set WGET_BINZIP=wget-1.11.4-1-bin.zip
set WGET_DEPZIP=wget-1.11.4-1-dep.zip

:: Maven data
set MAVEN_URL=http://mirror.mkhelif.fr/apache//maven/binaries/apache-maven-2.2.1-bin.zip
set MAVEN_ZIP=apache-maven-2.2.1-bin.zip
set M2_HOME=%TOOLS_FOLDER%\apache-maven-2.2.1
set M2=%M2_HOME%/bin
set MAVEN_OPTS=-Xms256m -Xmx512m
set MAVEN_M2=C:\Documents and Settings\%username%\.m2

:: Hsqldb data
set HSQLDB_URL=http://sourceforge.net/projects/hsqldb/files/hsqldb/hsqldb_2_0/hsqldb-2.0.0.zip/download

:: Log4j data
set LOG4J_URL=http://mir2.ovh.net/ftp.apache.org/dist//logging/log4j/1.2.16/apache-log4j-1.2.16.zip

echo ################################# INIT #################################

::Pause 2s
ping 127.0.0.1 -n 2 >NULL


echo.
echo **********************************************************************
echo INSTALLATION OF BASIC STUFF 
echo **********************************************************************
echo.
echo "Uninstalling jdk if already installed"
if exist "%JDK_MSIPKG%" msiexec /x "%JDK_MSIPKG%" /qn
echo Ok.

echo.
echo "Stopping first %MySQLSERVER_ServiceName% if existing"
net stop %MySQLSERVER_ServiceName% 2>NULL
echo Ok.

echo.
echo "Disabling Windows Firewall first if enabled"
netsh firewall set opmode disable

echo.
echo "Cleaning MySQL + JAVA + Tools folders"
if exist "%TOOLS_FOLDER%" rmdir /Q /S "%TOOLS_FOLDER%" 2>NULL
if exist "%DATA_FOLDER%"  rmdir /Q /S "%DATA_FOLDER%" 2>NULL
if exist "%MYSQL_FOLDER%" rmdir /Q /S "%MYSQL_FOLDER%" 2>NULL
if exist "%UNZIP_FOLDER%" rmdir /Q /S "%UNZIP_FOLDER%" 2>NULL
if exist "%JAVA_FOLDER%"  rmdir /Q /S "%JAVA_FOLDER%" 2>NULL
echo Ok.

echo.
echo "Creating MySQL + Maven + Java + Tools folders"
mkdir "%TOOLS_FOLDER%" 2>NULL
mkdir "%WGET_FOLDER%" 2>NULL
mkdir "%UNZIP_FOLDER%" 2>NULL
mkdir "%UNZIP_BINDIR%" 2>NULL
mkdir "%JAVA_BINDIR%" 2>NULL
echo Ok.


echo.
echo **********************************************************************
echo INSTALLATION OF UNZIP
echo **********************************************************************

echo.
echo "Copying needed tools (unzip)"
copy "unzip.exe" "%UNZIP_BINDIR%" 2>NULL

echo "According Global PATH variable for unzip"
set PATH=%PATH%;%UNZIP_BINDIR%
echo Ok.

::Pause 2s
ping 127.0.0.1 -n 2 >NULL


echo.
echo **********************************************************************
echo INSTALLATION OF WGET
echo **********************************************************************

echo.
echo "Copying needed tools (wget)"
copy "%WGET_BINZIP%" "%TOOLS_FOLDER%" 2>NULL
copy "%WGET_DEPZIP%" "%TOOLS_FOLDER%" 2>NULL
echo Ok.

cd %ROOT%
%ROOT%
cd "%WGET_FOLDER%"

echo.
echo "Unziping/Installing wget command"
unzip -qq "..\%WGET_BINZIP%"
unzip -qq "..\%WGET_DEPZIP%"

echo "According Global PATH variable for wget"
set PATH=%PATH%;%WGET_BINDIR%
echo Ok.

::Pause 2s
ping 127.0.0.1 -n 2 >NULL


echo.
echo **********************************************************************
echo INSTALLATION OF JDK
echo **********************************************************************
cd "%TOOLS_FOLDER%"
echo.
echo "Catching JDK exe package"
wget.exe "%JDK_URL%"

echo.
echo "Installing JDK package (jdk-6u21-windows-i586.exe - log is %JDK_LogFile%)"
"jdk-6u21-windows-i586.exe" /l* "%JDK_LogFile%" INSTALLDIR="%JDK_InstallDIR%" /qn
echo Ok.

echo.
echo "According Global PATH variable"
set PATH=%PATH%;%JAVA_BinDIR%
echo Ok.

echo.
echo "Checking java version"
java -version
echo Ok.

::Pause 2s
ping 127.0.0.1 -n 2 >NULL


echo.
echo **********************************************************************
echo INSTALLATION OF MYSQL (server)
echo **********************************************************************
echo.
echo "Catching mysql server msi package"
wget.exe "%MySQLSERVER_URL%"
move "%MySQLSERVER_TMP_MSI%" "%MySQLSERVER_MSI%"

echo.
echo "Installing mysql server msi package (%MySQLSERVER_MSI% - log is %MySQLSERVER_LogFile%)"
msiexec /i "%MySQLSERVER_MSI%" /l* "%MySQLSERVER_LogFile%" INSTALLDIR="%MySQLSERVER_InstallDIR%" DATADIR="%MySQLSERVER_DataDIR%" /qn
echo Ok.

echo.
echo "Copying my.ini (myssql server customized configuration file)"
copy "%MySQLSERVER_MyINI_FilePath_1%" "%MySQLSERVER_InstallDIR%"
echo Ok.

echo.
echo "Starting %MySQLSERVER_ServiceName%"
cd "%MySQLSERVER_BinDIR%"
mysqld --install %MySQLSERVER_ServiceName% --defaults-file="%MySQLSERVER_MyINI_FilePath_2%"
net start %MySQLSERVER_ServiceName% 2>NULL

echo "Creating DB NokiaTest"
mysqladmin -u %MySQLSERVER_USER% create NokiaTest
echo Ok.

echo.
echo "Updating password for user %MySQLSERVER_USER%"
mysqladmin -u %MySQLSERVER_USER% password %MySQLSERVER_PASSWD%
echo Ok.

echo.
echo "According Global PATH variable"
set PATH=%PATH%;%MySQLSERVER_BinDIR%
echo Ok.

echo.
echo "Checking mysql version"
mysql --version
echo Ok.

::Pause 2s
ping 127.0.0.1 -n 2 >NULL


echo.
echo **********************************************************************
echo INSTALLATION OF MYSQL (connector)
echo **********************************************************************
cd "%TOOLS_FOLDER%"
echo.
echo "Catching mysql connector"
wget.exe "%MySQL_Con_URL%"
echo "Unziping mysql connector"
unzip -qq "%MySQL_Con_ZIP%"
echo Ok.

::Pause 2s
ping 127.0.0.1 -n 2 >NULL


echo.
echo **********************************************************************
echo INSTALLATION OF MAVEN
echo **********************************************************************
cd "%TOOLS_FOLDER%"
echo.
echo "Catching maven binaries"
wget.exe "%MAVEN_URL%"

echo "Unziping maven binaries"
unzip -qq "%MAVEN_ZIP%"
echo Ok.

echo.
echo "According Global PATH variable for maven"
set PATH=%PATH%;%M2%
echo Ok.

echo.
echo "Checking maven version"
call mvn -version
echo Ok.

::Pause 2s
ping 127.0.0.1 -n 2 >NULL


echo.
echo **********************************************************************
echo INSTALLATION OF Log4J, sqqldb
echo **********************************************************************
cd "%TOOLS_FOLDER%"
echo "Catching log4j packages"
wget.exe "%HSQLDB_URL%"
echo "Catching log4j packages"
wget.exe "%LOG4J_URL%"
echo Ok.

::Pause 2s
ping 127.0.0.1 -n 2 >NULL


echo.
echo **********************************************************************
echo INSTALLATION OF POM of 3rd party locally to avoid maven fetch
echo **********************************************************************
:: hsqldb, mysqlconnector, log4j
echo.
echo "Maven - Installing mysqlconnector"
call mvn install:install-file -DgroupId=org.mysql.connector -DartifactId=mysqlconnector -Dversion=5.0.8 -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\ThirdParty\mysql-connector-java-5.0.8\mysql-connector-java-5.0.8-bin.jar -DpomFile=%WORKSPACE_FOLDER%\ThirdParty\poms\pomMysqlConnector.xml
echo.
echo "Maven - Installing log4j"
call mvn install:install-file -DgroupId=org.apache.log4j -DartifactId=log4j -Dversion=1.2.16 -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\ThirdParty\apache-log4j-1.2.16\log4j-1.2.16.jar -DpomFile=%WORKSPACE_FOLDER%\ThirdParty\poms\pomLog4j.xml
echo.
echo "Maven - Installing hsqldb"
call mvn install:install-file -DgroupId=org.hsqldb.db -DartifactId=hsqldb -Dversion=2.0.0 -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\ThirdParty\hsqldb\lib\hsqldb.jar -DpomFile=%WORKSPACE_FOLDER%\ThirdParty\poms\pomHsqldb.xml
echo.
echo "Maven - Installing sqltools"
call mvn install:install-file -DgroupId=org.hsqldb.db -DartifactId=sqltools -Dversion=2.0.0 -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\ThirdParty\hsqldb\lib\sqltool.jar -DpomFile=%WORKSPACE_FOLDER%\ThirdParty\poms\pomSqlTools.xml

:: xmlbeans.*
echo.
echo "Maven - Installing xbeans"
call mvn install:install-file -DgroupId=org.apache.xmlbeans -DartifactId=xbeans -Dversion=2.5.0 -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\ThirdParty\xmlBeans\lib\xbean.jar -DpomFile=%WORKSPACE_FOLDER%\ThirdParty\poms\pomXbeans.xml
echo.
echo "Maven - Installing jsr-api"
call mvn install:install-file -DgroupId=org.apache.xmlbeans -DartifactId=jsr-api -Dversion=2.5.0 -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\ThirdParty\xmlBeans\lib\jsr173_1.0_api.jar -DpomFile=%WORKSPACE_FOLDER%\ThirdParty\poms\pomJsrAPI.xml
echo.
echo "Maven - Installing resolver"
call mvn install:install-file -DgroupId=org.apache.xmlbeans -DartifactId=resolver -Dversion=2.5.0 -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\ThirdParty\xmlBeans\lib\resolver.jar -DpomFile=%WORKSPACE_FOLDER%\ThirdParty\poms\pomResolver.xml
echo.
echo "Maven - Installing xbeans-xpath"
call mvn install:install-file -DgroupId=org.apache.xmlbeans -DartifactId=xbeans-xpath -Dversion=2.5.0 -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\ThirdParty\xmlBeans\lib\xbean_xpath.jar -DpomFile=%WORKSPACE_FOLDER%\ThirdParty\poms\pomXbeansXPath.xml
echo.
echo "Maven - Installing xbeans-qname"
call mvn install:install-file -DgroupId=org.apache.xmlbeans -DartifactId=xbeans-qname -Dversion=2.5.0 -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\ThirdParty\xmlBeans\lib\xmlbeans-qname.jar -DpomFile=%WORKSPACE_FOLDER%\ThirdParty\poms\pomXbeans-qname.xml
echo.
echo "Maven - Installing xml-public"
call mvn install:install-file -DgroupId=org.apache.xmlbeans -DartifactId=xml-public -Dversion=2.5.0 -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\ThirdParty\xmlBeans\lib\xmlpublic.jar -DpomFile=%WORKSPACE_FOLDER%\ThirdParty\poms\pomXPublic.xml


:: Create a repository with all dependencies to be imported into eclispe 
echo.
echo "Maven - Uploading dependencies in eclipse layout - QuestionsType"
call mvn -f %WORKSPACE_FOLDER%\QuestionsType\pom.xml dependency:copy-dependencies jar:jar

echo.
echo "Maven - Installing QuestionType"
call mvn install:install-file -DgroupId=org.agile4techos.nokiatest -DartifactId=questionType -Dversion=1.0.0 -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\QuestionsType\src\questionType-1.0.0.jar -DpomFile=%WORKSPACE_FOLDER%\QuestionsType\pom.xml


echo.
echo "Maven - Uploading dependencies in eclipse layout - QuestionStorage"
call mvn -f %WORKSPACE_FOLDER%\QuestionStorage\pom.xml dependency:copy-dependencies jar:jar

echo.
echo "Maven - Installing QuestionStorage"
call mvn install:install-file -DgroupId=org.agile4techos.nokiatest -DartifactId=questionStorage -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\QuestionStorage\target\QuestionStorage-1.0-SNAPSHOT.jar -DpomFile=%WORKSPACE_FOLDER%\QuestionStorage\pom.xml


echo.
echo "Maven - Uploading dependencies in eclipse layout - StorageServer"
call mvn -f %WORKSPACE_FOLDER%\StorageServer\pom.xml dependency:copy-dependencies jar:jar

echo.
echo "Maven - Installing StorageServer"
call mvn install:install-file -DgroupId=org.agile4techos.nokiatest -DartifactId=storageServer -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\StorageServer\target\StorageServer-1.0-SNAPSHOT.jar -DpomFile=%WORKSPACE_FOLDER%\StorageServer\pom.xml


echo.
echo "Maven - Uploading dependencies in eclipse layout - StorageClient"
call mvn -f %WORKSPACE_FOLDER%\StorageClient\pom.xml dependency:copy-dependencies jar:jar

echo.
echo "Maven - Installing StorageClient"
call mvn install:install-file -DgroupId=org.agile4techos.nokiatest -DartifactId=storageClient -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\Storageclient\target\StorageClient-1.0-SNAPSHOT.jar -DpomFile=%WORKSPACE_FOLDER%\StorageClient\pom.xml


echo.
echo "Maven - Uploading dependencies in eclipse layout - NokiaTest_FE"
call mvn -f %WORKSPACE_FOLDER%\NokiaTest_FE\pom.xml dependency:copy-dependencies jar:jar

echo.
echo "Maven - Installing NokiaTest_FE"
call mvn install:install-file -DgroupId=org.agile4techos.nokiatest -DartifactId=NokiaTest_FE -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\NokiaTest_FE\target\nokiaTest_FE-1.0-SNAPSHOT.jar -DpomFile=%WORKSPACE_FOLDER%\NokiaTest_FE\pom.xml


echo.
echo "Maven - Uploading dependencies in eclipse layout - SimpleDemo_FR_BDD (bdd)"
call mvn -f %WORKSPACE_FOLDER%\SimpleDemo_FR_BDD\pom.xml dependency:copy-dependencies jar:jar

echo.
echo "Maven - Installing SimpleDemo_FR_BDD (bdd)"
call mvn install:install-file -DgroupId=org.agile4techos.nokiatest -DartifactId=bdd -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=%WORKSPACE_FOLDER%\SimpleDemo_FR_BDD\target\bdd-1.0-SNAPSHOT.jar -DpomFile=%WORKSPACE_FOLDER%\SimpleDemo_FR_BDD\pom.xml



echo **********************************************************************
echo EXECUTION OF FINAL CLEAN UP
echo **********************************************************************
echo.
echo "Removing zip files"
del "%TOOLS_FOLDER%\*.zip" 2>NULL

echo.
echo "Removing msi files"
del "%TOOLS_FOLDER%\*.msi" 2>NULL

echo.
echo "Removing exe files"
del "%TOOLS_FOLDER%\*.exe" 2>NULL

:: Catch Time when ending installation script
for /f "tokens=1 delims=: " %%h in ('echo %time%') do set end_hour=%%h 2>NULL
for /f "tokens=2 delims=: " %%m in ('echo %TIME%') do set end_minu=%%m 2>NULL
for /f "tokens=3 delims=: " %%s in ('echo %TIME%') do set end_secd=%%s 2>NULL

:: Conversion of both times
set /a hh1=60*60*%start_hour% 2>NULL
set /a hh2=60*60*%end_hour% 2>NULL
set /a mm1=60*%start_minu% 2>NULL
set /a mm2=60*%end_minu% 2>NULL
set /a ss1=%start_secd% 2>NULL
set /a ss2=%end_secd% 2>NULL

:: Add input hours , minutes , seconds 
set /a t1=hh1+mm1+ss1 2>NULL
set /a t2=hh2+mm2+ss2 2>NULL

:: Calculate difference in seconds
set /a ds=t2-t1 2>NULL
if %ds% LSS 0 set /a ds=%ds%+24*60*60 2>NULL

:: Caculate in time format time difference
set /a h=(%ds%/3600) 2>NULL
set /a m=(%ds%/60)-60*%h% 2>NULL
set /a s=%ds%-60*(%ds%/60) 2>NULL

:: Add leading zeros
IF %h% LSS 10 SET h=0%h% 2>NULL
IF %m% LSS 10 SET m=0%m% 2>NULL
IF %s% LSS 10 SET s=0%s% 2>NULL

echo.
echo "Script beginning time (hh:mm:ss) : %start_hour%:%start_minu%:%start_secd%"
echo "Script ending    time (hh:mm:ss) : %end_hour%:%end_minu%:%end_secd%"
echo "Script execution time (hh:mm:ss) : %h%:%m%:%s%"

:: removes NULL file
del %~dp0\NULL

echo "Operations ended successfully... enjoy!"
echo **********************************************************************

cd "%WORKSPACE_FOLDER%\Installation"